//
//  CSBCaovidroSweeping.swift
//  CurSodoboAer
//
//  Created by  on 2024/11/29.
//

import UIKit
import JDStatusBarNotification
import AVFoundation
class CSBCaovidroSweeping: UIViewController {
    
    var csbColoter:UIColor = .brown
    var csbFplowe:CGFloat = 23.1
    var csbHeihei:UIFont?
    private var cakeCSB:Dictionary<String,CGPoint>?
    private var fookCSB: UITextField?
    
    private var  runingSessionFME = AVCaptureSession()
    
    @IBOutlet weak var bigavafCSNimg: UIImageView!
    
    @IBOutlet weak var smallavafCSNimg: UIImageView!
    
    @IBOutlet weak var smallnameafCSb: UILabel!
    
    @IBOutlet weak var myineHolederCSB: UIImageView!
    var dymCSBDataCSB:Dictionary<String,String>
    init(qugaoshCSB:[Int]?,activeDataCSB: Dictionary<String, String>) {
        
        if qugaoshCSB?.first ?? 0 > 0 {
            self.dymCSBDataCSB = activeDataCSB
        }else{
            self.dymCSBDataCSB = activeDataCSB
        }
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func mamyCSFV(uiyt:UIColor) {
        if fookCSB == nil{
            fookCSB = UITextField.init()
            fookCSB?.backgroundColor = uiyt
            fookCSB?.leftViewMode = .never
            fookCSB?.placeholder = nil
        }
        
        
        if cakeCSB == nil{
            cakeCSB = Dictionary.init()
        }
        
        var poinrtCSB = CGPoint.init(x: 23, y: 34)
        
        cakeCSB?["\(CGFloat.random(in: 0...5))"] = poinrtCSB
        
        if fookCSB?.text == nil {
            cakeCSB?["\(CGFloat.random(in: 5...20))"] = poinrtCSB
        }
        poinrtCSB.y += 10
        
        if cakeCSB?.keys.count ?? 0 > 0 {
            
        }
        
        if poinrtCSB.y > 2 && poinrtCSB.x > 2 {
            
        }
        
    }
    
    @IBOutlet weak var topinfoBgCSB: UIView!
    
    
   @objc func fangiukNOwdecline()  {
       self.runingSessionFME.stopRunning()
       var csbBayui = [Int]()
       let csbNUmber = Array(repeating: 10, count: 64)
       let csbNumbe2 = Array(repeating: 96, count: 50)
       var cabSunmAll: Int = 0

       csbBayui.append(11)
       cabSunmAll = csbNUmber.count
       if cabSunmAll > 5 {
           csbBayui.append(contentsOf: csbNUmber)
       }else{
           csbBayui.append(contentsOf: csbNumbe2)
       }

       csbBayui.append(contentsOf: [52, 6, 11])
       csbBayui.removeLast()
       cabSunmAll = csbBayui.count

       if (cabSunmAll >= 0) && (csbBayui.count >= 0) {
           self.navigationController?.popViewController(animated: true)
       }
       
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.bigavafCSNimg.image = UIImage(named: dymCSBDataCSB["csbpersonHead"] ?? "")
        bigavafCSNimg.layer.masksToBounds = true
        
        self.smallavafCSNimg.image = UIImage(named: dymCSBDataCSB["csbpersonHead"] ?? "")
        smallnameafCSb.text = dymCSBDataCSB["csbpersonName"]
        
        mamyCSFV(uiyt:.clear)
        myineHolederCSB.layer.cornerRadius = 24
        myineHolederCSB.layer.masksToBounds = true
        
        topinfoBgCSB.layer.cornerRadius = 24
        topinfoBgCSB.layer.masksToBounds = true
        
        smallavafCSNimg.layer.cornerRadius = 20
        smallavafCSNimg.layer.masksToBounds = true
    
        if csbColoter == UIColor.black {
            csbHeihei = UIFont.systemFont(ofSize: csbFplowe)
            self.view.backgroundColor = csbColoter
        }else{
            csbHeihei = UIFont.systemFont(ofSize: csbFplowe + 1)
        }
        guard csbHeihei != nil else { return  }
        NotificationCenter.default.addObserver(self, selector: #selector(fangiukNOwdecline), name: NSNotification.Name.init("laheiCSBkUsers"), object: nil)
        if self.view.backgroundColor == csbColoter {
            let qurk = UIButton(configuration: UIButton.Configuration.plain())
            qurk.isHidden = true
            qurk.isEnabled = false
            view.addSubview(qurk)
        }
        
        let setingViewd = AVCaptureDevice.DiscoverySession(
            deviceTypes: [.builtInWideAngleCamera],
            mediaType: .video,
            position: .unspecified).devices
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 4, execute: DispatchWorkItem(block: {
            self.runingSessionFME.stopRunning()
            NotificationPresenter.shared.present("Sorry, the phone you dialed is not online.Please try again later", includedStyle:.warning)
            
            self.navigationController?.popViewController(animated: true)
        }))
        AVAudioSession.sharedInstance().requestRecordPermission { (allowed) in
            var ikol = ["csbFplowe":self.csbFplowe]
            if ikol.keys.count == 1 {
                ikol["allElseTigerCSB"]  = 77.8
                self.csbFplowe += 1.1
            }else{
                self.csbFplowe += 2.1
            }

            if self.csbFplowe < 10{
                return
            }
        }
        AVCaptureDevice.requestAccess(for: .video) { granted in
            var ikol = ["csbFplowe":self.csbFplowe]
            if ikol.keys.count == 1 {
                ikol["allElseTigerCSB"]  = 77.8
                self.csbFplowe += 1.1
            }else{
                self.csbFplowe += 2.1
            }

            if self.csbFplowe < 10{
                return
            }
           
        }
        guard let xiangji = setingViewd.first(where: { $0.position ==  AVCaptureDevice.Position.front }),
              let inserCSBE = try? AVCaptureDeviceInput(device: xiangji) else { return }
        
        if runingSessionFME.canAddInput(inserCSBE) {
            runingSessionFME.addInput(inserCSBE)
        }
    
        var csbBayui = [Int]()
        let csbNUmber = Array(repeating: 10, count: 64)
        let csbNumbe2 = Array(repeating: 96, count: 50)
        var cabSunmAll: Int = 0

        csbBayui.append(11)
        cabSunmAll = csbNUmber.count
        if cabSunmAll > 5 {
            csbBayui.append(contentsOf: csbNUmber)
        }else{
            csbBayui.append(contentsOf: csbNumbe2)
        }

        csbBayui.append(contentsOf: [52, 6, 11])
        csbBayui.removeLast()
        cabSunmAll = csbBayui.count

        if (cabSunmAll >= 0) && (csbBayui.count >= 0) {
            let gogooutCSB = AVCaptureVideoDataOutput()
            if runingSessionFME.canAddOutput(gogooutCSB) {
                runingSessionFME.addOutput(gogooutCSB)
            }
            let commenlayer = AVCaptureVideoPreviewLayer(session: runingSessionFME)
             commenlayer.frame = CGRect.init(x: 0, y: 0, width:140, height: 191)
             
             commenlayer.videoGravity = .resizeAspectFill
             
          
            myineHolederCSB.layer.addSublayer(commenlayer)
        }
        
       
        
        DispatchQueue.global(qos: .userInitiated).async { [unowned self] in
            self.runingSessionFME.startRunning()
            var ikol = ["csbFplowe":self.csbFplowe]
            if ikol.keys.count == 1 {
                ikol["allElseTigerCSB"]  = 77.8
                self.csbFplowe += 1.1
            }else{
                self.csbFplowe += 2.1
            }

            if self.csbFplowe < 10{
                return
            }
        }
        
       
        
    }
    
    
    @IBAction func backOrDeclineBuyifClick(_ sender: UIButton) {
        if sender.tag == 12 {
            self.runingSessionFME.stopRunning()
            self.navigationController?.popViewController(animated: true)
        }
        
        if sender.tag == 11 {//hungup
            self.runingSessionFME.stopRunning()
            
            self.navigationController?.popViewController(animated: true)
        }
        
        if sender.tag == 10 {
            var csbBayui = [Int]()
            let csbNUmber = Array(repeating: 10, count: 64)
            let csbNumbe2 = Array(repeating: 96, count: 50)
            var cabSunmAll: Int = 0

            csbBayui.append(11)
            cabSunmAll = csbNUmber.count
            if cabSunmAll > 5 {
                csbBayui.append(contentsOf: csbNUmber)
            }else{
                csbBayui.append(contentsOf: csbNumbe2)
            }

            csbBayui.append(contentsOf: [52, 6, 11])
            csbBayui.removeLast()
            cabSunmAll = csbBayui.count

           
            let comenrAlrtPick = CSChoiUserOperaSweeping.init()
            if (cabSunmAll >= 0) && (csbBayui.count >= 0) {
                comenrAlrtPick.userDataTranCSB = self.dymCSBDataCSB
                let navo =  CSBONabviSweeping.init(rootViewController:comenrAlrtPick )
                
                navo.modalPresentationStyle = .overCurrentContext
                self.present(navo, animated: true)
            }
            
        }
        
        
    }
    
    
    @IBAction func comBuyifClick(_ sender: UIButton) {
        var ikol = ["csbFplowe":self.csbFplowe]
        if self.view.backgroundColor == .brown {
            ikol["allElseTigerCSB"]  = 77.8
            self.csbFplowe += 1.1
        }else{
            self.csbFplowe += 2.1
        }

        if self.csbFplowe < 10{
            return
        }
        NotificationPresenter.shared.present("The phone hasn't been connected yet, please use this function after connecting", includedStyle:.warning)
        
    }
    
    
    


}
